package org.bet.dao.mongo;

import org.bet.dao.GenomeDao;
import org.bet.domain.Assembly;
import org.bet.domain.Genome;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

public class MongoDBGenomeDao implements GenomeDao {
	private MongoTemplate mongoTemplate;
	
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

	@Override
	public void saveOrUpdateGenome(Genome genome) {
		if(genome != null){

			mongoTemplate.remove(new Query(new Criteria("genomeId").is(genome.getId())), Assembly.class);
			mongoTemplate.insert(genome.getAssemblies(), Assembly.class);
			
//			for(Assembly assembly : genome.getAssemblies()){
//				mongoTemplate.save(assembly);
//			}
			
			mongoTemplate.save(genome);
		}
	}

	@Override
	public boolean existsGenome(String id) {
		return getGenome(id) != null;
	}

	@Override
	public Genome getGenome(String id) {
		Genome target = mongoTemplate.findOne(new Query(new Criteria("id").is(id)), Genome.class);
		
		// fetch syndicator
//		String syndicatorId = target.getSyndicator().getId();
//
//		Syndicator syndicator = mongoTemplate.findOne(new Query(new Criteria("id").is(syndicatorId )), Syndicator.class);
//		target.getSyndicator().setInstitution(syndicator.getInstitution());
//		target.getSyndicator().setEngineer(syndicator.getEngineer());
//		target.getSyndicator().setService(syndicator.getService());
		
		return target;
	}

}
